

const fp = require('lodash/fp')
const { Maybe, Container } = require('./support')

/**
 * 练习1：使用 fp.add(x,y) 和 fp.map(f,x) 创建一个
 * 能让 functor 里的值增加的函数ex1
 **/

let maybe = Maybe.of([5, 6, 1])

// let ex1 = maybe.map(item => {
//   return fp.map(val => {
//     return fp.add(1, val)
//   }, item)
// })
let ex1 = (num) => {
  let arr = maybe.map(item => fp.map(val => fp.add(num, val), item))
  // let arr = fp.map(elem => fp.add(num, elem), maybe._value)
  return arr
}
console.log(ex1(1))

/**
 * 练习2：实现一个函数ex2，能够使用fp.first 获取列表的 第一个元素
 **/
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so','la', 'ti', 'do'])

let ex2 = () => {
  // let value = xs.map(arr => fp.first(arr))
  let value = xs.map(fp.first)
  return value
}
console.log(ex2())

/**
 * 练习3：实现一个函数ex3，使用safeProp和fp.first找到user的名字的首字母
 **/

let safeProp = fp.curry(function (x, o) {
  return Maybe.of(o[x])
})


let user = { id: 2, name: 'Albert' }

let ex3 = () => {
  let newMaybe = safeProp('name', user)
  return newMaybe.map(item => fp.first(item))
}
console.log(ex3())

/**
 * 练习4：使用 Maybe 重写 ex4，不要有if 语句
 **/
let ex4 = function (n) {
  if (n) {
    return parseInt(n)
  }
}

// 重写 为 ex5
let ex5 = (val) => {
  let d = Maybe.of(val).map(item => parseInt(item))
  return d
}

console.log(ex4('1'))
console.log(ex5('1'))







